﻿using System;
using System.Collections.Generic;
using Duo1JHttpServer.Framework.Interface;
using Duo1JHttpServer.Framework.Model;
using Duo1JHttpServer.Framework.Utils;

namespace Duo1JHttpServer.Framework.Config
{
    /// <summary>
    /// 请求处理器映射
    /// </summary>
    public class HandlerConfig
    {
        private List<MapperConfigModel> mapperModels;
        private readonly Dictionary<string, IHttpHandle> handleDic = new Dictionary<string, IHttpHandle>();

        /// <summary>
        /// 映射处理器模型
        /// </summary>
        public List<MapperConfigModel> MapperModels
        {
            get { return mapperModels; }
            set
            {
                mapperModels = value;
                ConsoleUtils.WriteYellowLine("Refresh-HandlerConfig");
                HandleDic.Clear();
                foreach (MapperConfigModel model in mapperModels)
                {
                    if (model.Handle != null)
                    {
                        HandleDic.Add(model.Url, model.Handle);
                        ConsoleUtils.WriteTabLine($"Request Url add: {model.Url}");
                    }
                }
            }
        }

        /// <summary>
        /// 请求与处理器映射
        /// </summary>
        public Dictionary<string, IHttpHandle> HandleDic
        {
            get { return handleDic; }
        }
    }
}